/**
 * 
 */
package com.swim.natatoriuminfo.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.swim.common.utils.DateUtil;
import com.swim.natatoriuminfo.entity.ClickStats;
import com.swim.natatoriuminfo.entity.NatatoriumInfo;
import com.swim.natatoriuminfo.service.ClickStatsService;
import com.swim.natatoriuminfo.service.NatatoriumInfoService;

/**
 * 访问量统计Controller
 * @author ljw
 * @version 2022-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/natatoriuminfo/clickStats")
public class ClickStatsController extends BaseController {

	@Autowired
	private ClickStatsService clickStatsService;
	
	@Autowired
	private NatatoriumInfoService natatoriuminfoService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ClickStats get(String id, boolean isNewRecord) {
		return clickStatsService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(ClickStats clickStats, Model model) {
		model.addAttribute("clickStats", clickStats);
		return "swim/natatoriuminfo/clickStatsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ClickStats> listData(ClickStats clickStats, HttpServletRequest request, HttpServletResponse response) {
		if(StringUtils.isEmpty(clickStats.getYearMonthStr())) {//页面加载时调用，默认查询当月的数据
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			clickStats.setYearMonthStr(sdf.format(new Date()));
		}
		String[] yearMonth = clickStats.getYearMonthStr().split("-");
		int year = Integer.parseInt(yearMonth[0]);
		int month = Integer.parseInt(yearMonth[1]);
		clickStats.setCreateDateStartTime_gte(DateUtil.getFirstDayOfMonth1(year, month));
		clickStats.setCreateDateEndTime_lte(DateUtil.getLastDayOfMonth1(year, month));
		Page<ClickStats> page = clickStatsService.findPage(new Page<ClickStats>(request, response), clickStats); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = "form")
	public String form(ClickStats clickStats, Model model) {
		model.addAttribute("clickStats", clickStats);
		return "swim/natatoriuminfo/clickStatsForm";
	}

	/**
	 * 保存访问量统计
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ClickStats clickStats) {
		clickStatsService.save(clickStats);
		return renderResult(Global.TRUE, text("保存访问量统计成功！"));
	}
	
	/**
	 * 删除访问量统计
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ClickStats clickStats) {
		clickStatsService.delete(clickStats);
		return renderResult(Global.TRUE, text("删除访问量统计成功！"));
	}
	
	/**
	 * 导出访问量数据
	 * @param natatoriumInfoId  游泳馆信息id
	 * @param yearMonthStr  年月
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = "exportData")
	public void exportData(ClickStats clickStats, HttpServletResponse response) {
		NatatoriumInfo natatoriuminfo = natatoriuminfoService.get(clickStats.getNatatoriumInfoId(), false);
		String[] yearMonth = clickStats.getYearMonthStr().split("-");
		int year = Integer.parseInt(yearMonth[0]);
		int month = Integer.parseInt(yearMonth[1]);
		clickStats.setCreateDateStartTime_gte(DateUtil.getFirstDayOfMonth1(year, month));
		clickStats.setCreateDateEndTime_lte(DateUtil.getLastDayOfMonth1(year, month));
		List<ClickStats> clickStatsList = clickStatsService.findList(clickStats);
		String fileName = natatoriuminfo.getName() + year + "年" + month + "月访问量数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport(natatoriuminfo.getName() + year + "年" + month + "月访问量数据", ClickStats.class)){
			ee.setDataList(clickStatsList).write(response, fileName);
		}
	}
	
	/**
	 * 查询访问量统计数据
	 * @param natatoriumInfoId  游泳馆信息id
	 * @param yearMonthStr	年月
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@PostMapping(value = "findClickStatsCountData")
	@ResponseBody
	public String findClickStatsCountData(ClickStats clickStats) {
		if(StringUtils.isEmpty(clickStats.getYearMonthStr())) {//页面加载时调用，默认查询当月的数据
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			clickStats.setYearMonthStr(sdf.format(new Date()));
		}
		return renderResult(Global.TRUE, text("查询访问量统计数据成功！"), clickStatsService.findClickStatsCountData(clickStats));
	}
}